home *** CD-ROM | disk | FTP | other *** search
- /*
- FILE: unix.c
-
- Routines: This file contains the following routines:
- fileinit()
- eihalt()
- kbread()
- clksec()
- tmpfile()
- restore()
- stxrdy()
- disable()
- memstat()
- filedir()
- sysreset()
-
- Written by Mikel Matthews, N9DVG
- SYS5 stuff added by Jere Sandidge, K4FUM
- */
-
- #include <stdio.h>
- #include <signal.h>
- #include <termio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/times.h>
- #include <time.h>
-
- #include "global.h"
- #include "cmdparse.h"
- #include "iface.h"
- #include "unix.h"
- #include "ndir.h"
-
- #define MAXCMD 1024
-
- int asy_attach();
-
- extern struct cmds attab[];
- extern struct termio savecon;
-
- unsigned nasy;
-
- fileinit()
- {
- int el;
- char *ep, *cp, *malloc(), *getenv();
- extern char *startup, *config, *userfile, *hosts, *mailspool;
- extern char *mailqdir, *mailqueue, *routeqdir, *alias;
- #ifdef FINGER
- extern char *fingerpath;
- #endif
-
- /* Try to get home directory name */
- if ((ep = getenv("NETHOME")) == NULLCHAR) {
- if ((ep = getenv("HOME")) == NULLCHAR) {
- ep = ".";
- }
- }
- el = strlen(ep);
- /* Replace each of the file name strings with the complete path */
- if ((cp = malloc(el + strlen(startup) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, startup);
- startup = cp;
- }
-
- if ((cp = malloc(el + strlen(config) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, config);
- config = cp;
- }
-
- if ((cp = malloc(el + strlen(userfile) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, userfile);
- userfile = cp;
- }
-
- if ((cp = malloc(el + strlen(hosts) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, hosts);
- hosts = cp;
- }
-
- if ((cp = malloc(el + strlen(alias) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, alias);
- alias = cp;
- }
-
- #ifdef FINGER
- if ((cp = malloc(el + strlen(fingerpath) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, fingerpath);
- fingerpath = cp;
- }
- #endif
-
- /* Try to get home directory name */
- if ((ep = getenv("NETSPOOL")) == NULLCHAR)
- ep = "/usr/spool";
- el = strlen(ep);
-
- if ((cp = malloc(el + strlen(mailspool) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, mailspool);
- mailspool = cp;
- }
-
- if ((cp = malloc(el + strlen(mailqdir) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, mailqdir);
- mailqdir = cp;
- }
-
- if ((cp = malloc(el + strlen(mailqueue) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, mailqueue);
- mailqueue = cp;
- }
-
- if ((cp = malloc(el + strlen(routeqdir) + 2)) == NULL)
- perror("malloc");
- else {
- sprintf(cp, "%s/%s", ep, routeqdir);
- routeqdir = cp;
- }
-
- }
-
- /* action routine for remote reset */
- sysreset()
- {
- extern char *netexe;
-
- execl(netexe,netexe,0);
- execl("net","net",0);
- printf("reset failed: exiting\n");
- exit(1);
- }
-
- eihalt()
- {
- tnix_scan();
- }
-
-
- kbread()
- {
- unsigned char c;
-
- if (read(fileno(stdin), &c, 1) <= 0)
- return -1;
-
- return ((int) c);
- }
-
-
- clksec()
- {
- long tim;
-
- (void) time(&tim);
-
- return ((int) tim);
- }
-
-
- restore()
- {
- }
-
-
- stxrdy()
- {
- return 1;
- }
-
-
- disable()
- {
- }
-
-
- memstat()
- {
- return 0;
- }
-
-
- /* wildcard filename lookup */
- filedir(name, times, ret_str)
- char *name;
- int times;
- char *ret_str;
- {
- static char dname[128], fname[128];
- static DIR *dirp = NULL;
- struct direct *dp;
- struct stat sbuf;
- char *cp, temp[128];
-
- /*
- * Make sure that the NULL is there in case we don't find anything
- */
- ret_str[0] = '\0';
-
- if (times == 0) {
- /* default a null name to *.* */
- if (name == NULL || *name == '\0')
- name = "*.*";
- /* split path into directory and filename */
- if ((cp = strrchr(name, '/')) == NULL) {
- strcpy(dname, ".");
- strcpy(fname, name);
- } else {
- strcpy(dname, name);
- dname[cp - name] = '\0';
- strcpy(fname, cp + 1);
- /* root directory */
- if (dname[0] == '\0')
- strcpy(dname, "/");
- /* trailing '/' */
- if (fname[0] == '\0')
- strcpy(fname, "*.*");
- }
- /* close directory left over from another call */
- if (dirp != NULL)
- closedir(dirp);
- /* open directory */
- if ((dirp = opendir(dname)) == NULL) {
- printf("Could not open DIR (%s)\n", dname);
- return;
- }
- } else {
- /* for people who don't check return values */
- if (dirp == NULL)
- return;
- }
-
- /* scan directory */
- while ((dp = readdir(dirp)) != NULL) {
- /* test for name match */
- if (wildmat(dp->d_name, fname)) {
- /* test for regular file */
- sprintf(temp, "%s/%s", dname, dp->d_name);
- if (stat(temp, &sbuf) < 0)
- continue;
- if ((sbuf.st_mode & S_IFMT) != S_IFREG)
- continue;
- strcpy(ret_str, dp->d_name);
- break;
- }
- }
-
- /* close directory if we hit the end */
- if (dp == NULL) {
- closedir(dirp);
- dirp = NULL;
- }
- }
-
-
- /* checks the time then ticks and updates ISS */
- void
- check_time()
- {
- int32 iss();
- long times();
-
- struct tms tb;
- static long clkval;
- long ntime, offset;
-
- /* read elapsed real time (typ. 60 Hz) */
- ntime = times(&tb);
-
- /* resynchronize if the error is large (10 seconds or more) */
- offset = ntime - clkval;
- if (offset > (10000/MSPTICK) || offset < 0)
- clkval = ntime;
-
- /* Handle possibility of several missed ticks */
- while (ntime != clkval) {
- ++clkval;
- icmpclk();
- tick();
- (void) iss();
- }
- }
-
-
- getds()
- {
- return 0;
- }
-
-
- audit()
- {
- }
-
-
- doshell(argc, argv)
- char **argv;
- {
- int i, stat;
- char str[MAXCMD];
- char *cp;
- struct termio tt_config;
-
- char *getenv();
-
- str[0] = '\0';
- for (i = 1; i < argc; i++) {
- strcat(str, argv[i]);
- strcat(str, " ");
- }
-
- ioctl(0, TCGETA, &tt_config);
- ioctl(0, TCSETAW, &savecon);
-
- if (argc > 1)
- stat = system(str);
- else if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
- stat = system(cp);
- else
- stat = system("exec /bin/sh");
-
- ioctl(0, TCSETAW, &tt_config);
-
- return stat;
- }
-
-
- dodir(argc, argv)
- int argc;
- char **argv;
- {
- int i, stat;
- char str[MAXCMD];
- struct termio tt_config;
-
- strcpy(str, "ls -l ");
- for (i = 1; i < argc; i++) {
- strcat(str, argv[i]);
- strcat(str, " ");
- }
-
- ioctl(0, TCGETA, &tt_config);
- ioctl(0, TCSETAW, &savecon);
-
- stat = system(str);
-
- ioctl(0, TCSETAW, &tt_config);
-
- return stat;
- }
-
-
- rename(s1, s2)
- char *s1, *s2;
- {
- char tmp[MAXCMD];
-
- (void) sprintf(tmp, "mv %s %s", s1, s2);
- (void) system(tmp);
- }
-
-
- int
- docd(argc, argv)
- int argc;
- char **argv;
- {
- char tmp[MAXCMD];
- char *getcwd();
-
- if (argc > 1) {
- if (chdir(argv[1]) == -1) {
- printf("Can't change directory\n");
- return 1;
- }
- }
- if (getcwd(tmp, sizeof(tmp)) == NULL)
- printf("%s\n", tmp);
-
- return 0;
- }
-
-
- ether_dump()
- {
- }
-
-
- mkdir(s, m)
- char *s;
- int m;
- {
- char tmp[MAXCMD];
-
- sprintf(tmp, "mkdir %s", s);
- if (system(tmp))
- return -1;
- if (chmod(s, m) < 0)
- return -1;
-
- return 0;
- }
-
-
- rmdir(s)
- char *s;
- {
- char tmp[MAXCMD];
-
- sprintf(tmp, "rmdir %s", s);
- if (system(tmp))
- return -1;
-
- return 0;
- }
-